package com.uff.hmstpa.controller.impl;

import com.uff.hmstpa.controller.ScheduleSystemController;
import com.uff.hmstpa.model.ScheduleSystem;
import com.uff.hmstpa.model.impl.ScheduleSystemImpl;
import com.uff.hmstpa.model.domain.entity.AbstractDoctor;
import com.uff.hmstpa.model.domain.valueobject.Appointment;
import com.uff.hmstpa.model.domain.valueobject.Patient;
import com.uff.hmstpa.model.persistence.AppointmentDAO;
import com.uff.hmstpa.model.persistence.DoctorDAO;
import com.uff.hmstpa.model.persistence.PatientDAO;
import com.uff.hmstpa.model.persistence.impl.AppointmentDAOImpl;
import com.uff.hmstpa.model.persistence.impl.DoctorDAOImpl;
import com.uff.hmstpa.model.persistence.impl.PatientDAOImpl;
import com.uff.hmstpa.vision.impl.ScheduleSystemUI;
import java.util.ArrayList;
import java.util.List;
import org.joda.time.DateTime;

public class ScheduleSystemControllerImpl implements ScheduleSystemController {

    private ScheduleSystem model;
    private ScheduleSystemUI view;
    private DoctorDAO doctorDAO = DoctorDAOImpl.getInstance();
    private PatientDAO patientDAO = PatientDAOImpl.getInstance();
    private AppointmentDAO appointmentDAO = AppointmentDAOImpl.getInstance();

    public ScheduleSystemControllerImpl(ScheduleSystemImpl model) {
        this.model = model;
        view = new ScheduleSystemUI(model, this);
    }

    public void init() {
        List<AbstractDoctor> doctors = DoctorDAOImpl.getInstance().load();
        for (int i = 0; i < doctors.size(); i++) {
            addDoctor(doctors.get(i));
        }
    }

    public void createDoctor(String name) {
        AbstractDoctor newDoctor = model.createDoctor(name);
        addDoctor(newDoctor);
    }

    public void addDoctor(AbstractDoctor doctor) {
        model.addDoctor(doctor);
    }

    public ArrayList<AbstractDoctor> retrieveDoctors() {
        return model.retrieveDoctors();
    }

    public void setSelectedDoctor(String doctorName) {
        if (doctorName == null) {
            model.setSelectedDoctor(null);
            view.updateTitle(view.getDefaultTitle() + " - All Appointments");
        } else {
            List<AbstractDoctor> doctors = model.retrieveDoctors();

            for (int i = 0; i < doctors.size(); i++) {
                if (doctors.get(i).getName().equalsIgnoreCase(doctorName)) {
                    model.setSelectedDoctor(doctors.get(i));
                    view.updateTitle(view.getDefaultTitle() + " - " + doctors.get(i).getName() + " Appointments");
                }
            }
        }

        view.refreshScreen();
    }

    public AbstractDoctor getSelectedDoctor() {
        return model.getSelectedDoctor();
    }

    public List<Appointment> retrieveAppointmentList(AbstractDoctor doctor) {
        List<Appointment> appointments = null;

        if (doctor != null) {
            appointments = appointmentDAO.loadAppointments(doctor.getCRM());
        } else {
            appointments = appointmentDAO.loadAppointments(null);
        }

        return appointments;
    }

    public Patient retrievePatient(String cpf) {
        return patientDAO.retrieve(cpf);
    }

    public AbstractDoctor retrieveDoctor(String crm) {
        return doctorDAO.retrieve(crm);
    }

    public Appointment retrieveAppointment(String crm, String cpf, DateTime time) {
        return appointmentDAO.retrieve(crm, cpf, time);
    }
}
